package com.lucence.d_optimize;

import java.io.IOException;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogDocMergePolicy;
import org.apache.lucene.index.MergeScheduler;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
import org.junit.Test;

import com.lucence.a_helloword.Article;
import com.lucence.util.ArticleDocument;
import com.lucence.util.Config;

public class TestOptimize {
	
	@Test
	public void testOptimize() throws Exception{
		
		for (int i = 0; i < 225; i++) {
			IndexWriterConfig indexWriterConfig = 
					new IndexWriterConfig(Version.LUCENE_36, Config.getAnalyzer());
			IndexWriter indexWriter = 
					new IndexWriter(Config.getDirectory(),
							indexWriterConfig);
			Article article = new Article();
			article.setId(i+"");
			article.setTitle("Lucene is a sousou");
			article.setContent("Lucene is apache");
			indexWriter.addDocument(ArticleDocument.articleToDocument(article));
			indexWriter.optimize();
			indexWriter.close();
			System.out.println(indexWriter.isLocked(Config.getDirectory()));
		}
	}
	
	@Test
	public void testMergerFactory() throws Exception{
		for (int i = 0; i < 4; i++) {
			IndexWriterConfig indexWriterConfig = 
					new IndexWriterConfig(Version.LUCENE_36, Config.getAnalyzer());
			
			LogDocMergePolicy  mergePolicy = new LogDocMergePolicy();
			mergePolicy.setMergeFactor(3);
			indexWriterConfig.setMergePolicy(mergePolicy);
			
			IndexWriter indexWriter = 
					new IndexWriter(Config.getDirectory(),
							indexWriterConfig);
			Article article = new Article();
			article.setId(i+"");
			article.setTitle("Lucene is a sousou");
			article.setContent("Lucene is apache");
			indexWriter.addDocument(ArticleDocument.articleToDocument(article));
			indexWriter.close();
			System.out.println(indexWriter.isLocked(Config.getDirectory()));
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
}	
